﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FormsAuthentication.SignOut();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //验证码
            if (!txtValidateCode.Text.Equals(Session["Login_ValidateCode"].ToString(), StringComparison.OrdinalIgnoreCase))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "ValidateFailt", "alert('验证码不正确!');", true);
                return;
            }
            string Account = txtUserName.Text.Trim();
            string Password = txtPassword.Text.Trim();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "Account";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Account";
            parm.ParameterValue = Account;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.ColumnName = "Password";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Password";
            parm.ParameterValue = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            SysUserBll bll = new SysUserBll();
            SysUserEntity entity = bll.Get<SysUserEntity>(parms);
            if (entity != null)
            {
                UserTokenData user = new UserTokenData(entity.UserID, entity.UserName, entity.Account,Guid.Empty,string.Empty,string.Empty,string.Empty,0);

                string userData = user.GetUserData();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                               1, Account,
                               DateTime.Now, DateTime.Now.AddDays(1),
                               true, userData);
                string token = FormsAuthentication.Encrypt(ticket);

                HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, token);
                userCookie.Expires = ticket.Expiration;
                Response.Cookies.Add(userCookie);

                Response.Redirect(FormsAuthentication.GetRedirectUrl(Account, true));
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "LoginFailt", "alert('用户名或密码错误');", true);
            }
        }
    }
}